﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Common.Class;
using Extra.ERP;
using Security.ERP;

namespace Dictionary
{
    public class xucMachineAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_MACHINE item);

        private readonly IContainer components = null;

        private ComboBoxEdit cboCom;
        private ComboBoxEdit cboPortType;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private LabelControl labelControl5;
        private TextEdit txtIP;
        private TextEdit txtPassword;
        private TextEdit txtPortID;

        public xucMachineAdd()
        {
            InitializeComponent();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_MACHINE().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtPortID.Text = "";
            txtIP.Text = "";
            txtPassword.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucMachineAdd));
            cboPortType = new ComboBoxEdit();
            txtPortID = new TextEdit();
            txtIP = new TextEdit();
            cboCom = new ComboBoxEdit();
            labelControl1 = new LabelControl();
            labelControl2 = new LabelControl();
            labelControl3 = new LabelControl();
            labelControl4 = new LabelControl();
            labelControl5 = new LabelControl();
            txtPassword = new TextEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            cboPortType.Properties.BeginInit();
            txtPortID.Properties.BeginInit();
            txtIP.Properties.BeginInit();
            cboCom.Properties.BeginInit();
            txtPassword.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(labelControl5);
            base.gcInfo.Controls.Add(labelControl4);
            base.gcInfo.Controls.Add(labelControl3);
            base.gcInfo.Controls.Add(labelControl2);
            base.gcInfo.Controls.Add(txtIP);
            base.gcInfo.Controls.Add(cboPortType);
            base.gcInfo.Controls.Add(txtPortID);
            base.gcInfo.Controls.Add(labelControl1);
            base.gcInfo.Controls.Add(cboCom);
            base.gcInfo.Controls.Add(txtPassword);
            base.gcInfo.Dock = DockStyle.Top;
            base.gcInfo.Location = new Point(0, 0);
            base.gcInfo.Size = new Size(0x15a, 0xc3);
            base.gcInfo.Controls.SetChildIndex(txtPassword, 0);
            base.gcInfo.Controls.SetChildIndex(cboCom, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl1, 0);
            base.gcInfo.Controls.SetChildIndex(txtPortID, 0);
            base.gcInfo.Controls.SetChildIndex(cboPortType, 0);
            base.gcInfo.Controls.SetChildIndex(txtIP, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl2, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl3, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl4, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl5, 0);
            base.txtNAME.Location = new Point(0x4a, 0x41);
            base.txtNAME.Size = new Size(0xff, 20);
            base.lblName.Location = new Point(8, 0x44);
            base.lblName.Size = new Size(0x3a, 13);
            base.lblName.Text = "T\x00ean thiết bị:";
            base.txtID.Location = new Point(0x4a, 0x23);
            base.txtID.Size = new Size(0xff, 20);
            base.lblId.Location = new Point(8, 0x26);
            base.lblId.Size = new Size(0x36, 14);
            base.lblId.Text = "M\x00e3 thiết bị:";
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x30, 0xd0);
            base.btnCancel.Location = new Point(0xef, 0xd0);
            base.btnSaveNew.Location = new Point(0x92, 0xd0);
            cboPortType.EditValue = "TCP/TP";
            cboPortType.Enabled = false;
            cboPortType.Location = new Point(0x4a, 0x60);
            cboPortType.Name = "cboPortType";
            cboPortType.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            cboPortType.Properties.Items.AddRange(new object[] {"Serial Port/RS485", "TCP/TP", "USB"});
            cboPortType.Size = new Size(100, 20);
            cboPortType.TabIndex = 4;
            txtPortID.Location = new Point(0xe5, 0x60);
            txtPortID.Name = "txtPortID";
            txtPortID.Size = new Size(100, 20);
            txtPortID.TabIndex = 5;
            txtIP.Location = new Point(0x4a, 0x7f);
            txtIP.Name = "txtIP";
            txtIP.Size = new Size(0xff, 20);
            txtIP.TabIndex = 6;
            cboCom.EditValue = "COM1";
            cboCom.Enabled = false;
            cboCom.Location = new Point(0xe5, 0x9e);
            cboCom.Name = "cboCom";
            cboCom.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            cboCom.Properties.Items.AddRange(new object[]
            {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10"});
            cboCom.Size = new Size(100, 20);
            cboCom.TabIndex = 8;
            labelControl1.Location = new Point(8, 0x63);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(0x31, 13);
            labelControl1.TabIndex = 9;
            labelControl1.Text = "Loại cổng:";
            labelControl2.Location = new Point(180, 0x63);
            labelControl2.Name = "labelControl2";
            labelControl2.Size = new Size(0x2b, 13);
            labelControl2.TabIndex = 10;
            labelControl2.Text = "Cổng số:";
            labelControl3.Location = new Point(8, 130);
            labelControl3.Name = "labelControl3";
            labelControl3.Size = new Size(0x31, 13);
            labelControl3.TabIndex = 11;
            labelControl3.Text = "Địa chỉ IP:";
            labelControl4.Location = new Point(8, 0xa1);
            labelControl4.Name = "labelControl4";
            labelControl4.Size = new Size(0x30, 13);
            labelControl4.TabIndex = 12;
            labelControl4.Text = "Mật khẩu:";
            labelControl5.Location = new Point(0xc6, 0xa1);
            labelControl5.Name = "labelControl5";
            labelControl5.Size = new Size(0x19, 13);
            labelControl5.TabIndex = 13;
            labelControl5.Text = "Com:";
            var bits = new int[4];
            txtPassword.EditValue = new decimal(bits);
            txtPassword.Location = new Point(0x4a, 0x9e);
            txtPassword.Name = "txtPassword";
            txtPassword.Properties.PasswordChar = '*';
            txtPassword.Size = new Size(100, 20);
            txtPassword.TabIndex = 7;
            txtPassword.TabStop = false;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucMachineAdd";
            base.Size = new Size(0x15a, 0xf5);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            cboPortType.Properties.EndInit();
            txtPortID.Properties.EndInit();
            txtIP.Properties.EndInit();
            cboCom.Properties.EndInit();
            txtPassword.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_MACHINE item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_MACHINE item)
        {
            base.txtID.Text = item.MachineCode;
            SYS_LOG.Insert("Danh Mục Ca L\x00e0m Việc", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.MachineName;
            cboPortType.Text = item.PortType;
            txtPortID.Text = item.PortID;
            txtIP.Text = item.IP;
            txtPassword.Text = item.Password;
            cboCom.Text = item.Com;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_machine = new DIC_MACHINE();
                if (dic_machine.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_machine = new DIC_MACHINE();
                if ((base.m_Status == Actions.Add) && dic_machine.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_MACHINE
            {
                MachineCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiMachine") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Ca L\x00e0m Việc", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_MACHINE(base.txtID.Text, base.txtNAME.Text, cboPortType.Text, txtPortID.Text, txtIP.Text,
                txtPassword.Text, cboCom.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiMachine") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Ca L\x00e0m Việc", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_MACHINE(base.txtID.Text, base.txtNAME.Text, cboPortType.Text, txtPortID.Text, txtIP.Text,
                txtPassword.Text, cboCom.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}